<template>
  <div>
    <div class="gva-form-box">
      <el-form :model="formData" ref="elFormRef" label-position="right" :rules="rule" label-width="80px">
        <el-form-item label="访客姓名:" prop="username">
          <el-input v-model="formData.username" :clearable="true" placeholder="请输入" />
       </el-form-item>
        <el-form-item label="访客性别:" prop="sex">
          <el-switch v-model="formData.sex" active-color="#13ce66" inactive-color="#ff4949" active-text="是" inactive-text="否" clearable ></el-switch>
       </el-form-item>
        <el-form-item label="手机号:" prop="tel">
          <el-input v-model="formData.tel" :clearable="true" placeholder="请输入" />
       </el-form-item>
        <el-form-item label="公司:" prop="company">
          <el-input v-model="formData.company" :clearable="true" placeholder="请输入" />
       </el-form-item>
        <el-form-item label="访客类型:" prop="visittype">
          <el-switch v-model="formData.visittype" active-color="#13ce66" inactive-color="#ff4949" active-text="是" inactive-text="否" clearable ></el-switch>
       </el-form-item>
        <el-form-item label="设备名称:" prop="device">
          <el-input v-model="formData.device" :clearable="true" placeholder="请输入" />
       </el-form-item>
        <el-form-item label="被访人姓名:" prop="touser">
          <el-input v-model="formData.touser" :clearable="true" placeholder="请输入" />
       </el-form-item>
        <el-form-item label="被访人电话:" prop="totel">
          <el-input v-model="formData.totel" :clearable="true" placeholder="请输入" />
       </el-form-item>
        <el-form-item label="访问原因:" prop="visitreason">
          <el-input v-model="formData.visitreason" :clearable="true" placeholder="请输入" />
       </el-form-item>
        <el-form-item label="同行状态:" prop="isvisited">
          <el-switch v-model="formData.isvisited" active-color="#13ce66" inactive-color="#ff4949" active-text="是" inactive-text="否" clearable ></el-switch>
       </el-form-item>
        <el-form-item label="访问开始时间:" prop="start">
          <el-date-picker v-model="formData.start" type="date" placeholder="选择日期" :clearable="true"></el-date-picker>
       </el-form-item>
        <el-form-item label="访问结束时间:" prop="end">
          <el-date-picker v-model="formData.end" type="date" placeholder="选择日期" :clearable="true"></el-date-picker>
       </el-form-item>
        <el-form-item label="通行照片:" prop="passimg">
          <el-input v-model="formData.passimg" :clearable="true" placeholder="请输入" />
       </el-form-item>
        <el-form-item label="通行方式:" prop="passway">
          <el-switch v-model="formData.passway" active-color="#13ce66" inactive-color="#ff4949" active-text="是" inactive-text="否" clearable ></el-switch>
       </el-form-item>
        <el-form-item label="通行目标:" prop="passgoal">
          <el-switch v-model="formData.passgoal" active-color="#13ce66" inactive-color="#ff4949" active-text="是" inactive-text="否" clearable ></el-switch>
       </el-form-item>
        <el-form-item label="touserjob字段:" prop="touserjob">
          <el-input v-model="formData.touserjob" :clearable="true" placeholder="请输入" />
       </el-form-item>
        <el-form-item label="备注:" prop="remark">
          <el-input v-model="formData.remark" :clearable="true" placeholder="请输入" />
       </el-form-item>
        <el-form-item label="选择区域:" prop="area">
          <el-switch v-model="formData.area" active-color="#13ce66" inactive-color="#ff4949" active-text="是" inactive-text="否" clearable ></el-switch>
       </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="save">保存</el-button>
          <el-button type="primary" @click="back">返回</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import {
  createVisit,
  updateVisit,
  findVisit
} from '@/api/visit'

defineOptions({
    name: 'VisitForm'
})

// 自动获取字典
import { getDictFunc } from '@/utils/format'
import { useRoute, useRouter } from "vue-router"
import { ElMessage } from 'element-plus'
import { ref, reactive } from 'vue'

const route = useRoute()
const router = useRouter()

const type = ref('')
const formData = ref({
            username: '',
            sex: false,
            tel: '',
            company: '',
            visittype: false,
            device: '',
            touser: '',
            totel: '',
            visitreason: '',
            isvisited: false,
            start: new Date(),
            end: new Date(),
            passimg: '',
            passway: false,
            passgoal: false,
            touserjob: '',
            remark: '',
            area: false,
        })
// 验证规则
const rule = reactive({
})

const elFormRef = ref()

// 初始化方法
const init = async () => {
 // 建议通过url传参获取目标数据ID 调用 find方法进行查询数据操作 从而决定本页面是create还是update 以下为id作为url参数示例
    if (route.query.id) {
      const res = await findVisit({ ID: route.query.id })
      if (res.code === 0) {
        formData.value = res.data.revisit
        type.value = 'update'
      }
    } else {
      type.value = 'create'
    }
}

init()
// 保存按钮
const save = async() => {
      elFormRef.value?.validate( async (valid) => {
         if (!valid) return
            let res
           switch (type.value) {
             case 'create':
               res = await createVisit(formData.value)
               break
             case 'update':
               res = await updateVisit(formData.value)
               break
             default:
               res = await createVisit(formData.value)
               break
           }
           if (res.code === 0) {
             ElMessage({
               type: 'success',
               message: '创建/更改成功'
             })
           }
       })
}

// 返回按钮
const back = () => {
    router.go(-1)
}

</script>

<style>
</style>
